From 07af6fdd4a48a9680a2a06c7d56ee07c15351292 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 19 Mar 2014 17:57:12 -0700 Subject: [PATCH] A passing test --- Makefile | 2 +- src/bin/cargo-compile.rs | 1 + src/bin/cargo-read-manifest.rs | 3 +- src/bin/cargo-rustc.rs | 26 ++++++++++++--- src/bin/cargo-verify-project.rs | 1 + src/cargo/mod.rs | 2 ++ src/cargo/util/mod.rs | 2 +- src/cargo/util/process_builder.rs | 55 ++++++++++++++++++++++++++----- tests/support.rs | 11 +++++-- tests/test_cargo_compile.rs | 37 +++++++++++++++++---- tests/tests.rs | 1 + 11 files changed, 114 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 71c621d3e..99b7559f8 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ tests/tests: $(BIN_TARGETS) $(HAMCREST) $(TEST_SRC) $(RUSTC) --test --crate-type=lib $(TEST_DEPS) -Ltarget --out-dir tests tests/tests.rs test-integration: tests/tests - tests/tests + CARGO_BIN_PATH=$(PWD)/target/ tests/tests test: test-integration diff --git a/src/bin/cargo-compile.rs b/src/bin/cargo-compile.rs index 39e885b23..34ca4ee19 100644 --- a/src/bin/cargo-compile.rs +++ b/src/bin/cargo-compile.rs @@ -1,4 +1,5 @@ #[crate_id="cargo-compile"]; +#[allow(deprecated_owned_vector)]; extern crate serialize; extern crate hammer; diff --git a/src/bin/cargo-read-manifest.rs b/src/bin/cargo-read-manifest.rs index e7d9fc0d1..5e99cf56f 100644 --- a/src/bin/cargo-read-manifest.rs +++ b/src/bin/cargo-read-manifest.rs @@ -1,4 +1,5 @@ #[crate_id="cargo-read-manifest"]; +#[allow(deprecated_owned_vector)]; extern crate cargo; extern crate hammer; @@ -111,7 +112,7 @@ fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExec let b = b_ref.clone(); let mut path = b.path.clone(); if path.is_none() { - path = Some(format!("src/bin/{}.rs", b.name.clone())); + path = Some(format!("src/{}.rs", b.name.clone())); } ExecTarget{ path: path.unwrap(), name: b.name } }); diff --git a/src/bin/cargo-rustc.rs b/src/bin/cargo-rustc.rs index 4e7778655..4e55d37e9 100644 --- a/src/bin/cargo-rustc.rs +++ b/src/bin/cargo-rustc.rs @@ -1,4 +1,5 @@ #[crate_id="cargo-rustc"]; +#[allow(deprecated_owned_vector)]; extern crate toml; extern crate serialize; @@ -10,7 +11,7 @@ use std::io::process::{Process,ProcessConfig,InheritFd}; use serialize::json; use serialize::Decodable; use std::path::Path; -use cargo::{Manifest,CargoResult,ToCargoError}; +use cargo::{Manifest,CargoResult,CargoError,ToCargoError}; /** cargo-rustc -- ...args @@ -21,7 +22,7 @@ use cargo::{Manifest,CargoResult,ToCargoError}; fn main() { match execute() { Err(e) => { - println!("{}", e.message); + write!(&mut std::io::stderr(), "{}", e.message); // TODO: Exit with error code }, _ => return @@ -36,16 +37,23 @@ fn execute() -> CargoResult<()> { let mut decoder = json::Decoder::new(json); let manifest: Manifest = Decodable::decode(&mut decoder); - let Manifest{ root, lib, .. } = manifest; + let Manifest{ root, lib, bin, .. } = manifest; + + let (crate_type, out_dir) = if lib.len() > 0 { + ( ~"lib", lib[0].path ) + } else if bin.len() > 0 { + ( ~"bin", bin[0].path ) + } else { + return Err(CargoError::new(~"bad manifest, no lib or bin specified", 1)); + }; let root = Path::new(root); - let out_dir = lib[0].path; let target = join(&root, ~"target"); let args = [ join(&root, out_dir), ~"--out-dir", target, - ~"--crate-type", ~"lib" + ~"--crate-type", crate_type ]; match io::fs::mkdir_recursive(&root.join("target"), io::UserRWX) { @@ -75,3 +83,11 @@ fn execute() -> CargoResult<()> { fn join(path: &Path, part: ~str) -> ~str { format!("{}", path.join(part).display()) } + +fn vec_idx(v: ~[T], idx: uint) -> Option { + if idx < v.len() { + Some(v[idx]) + } else { + None + } +} diff --git a/src/bin/cargo-verify-project.rs b/src/bin/cargo-verify-project.rs index 7ee47ae88..255a585e0 100644 --- a/src/bin/cargo-verify-project.rs +++ b/src/bin/cargo-verify-project.rs @@ -1,4 +1,5 @@ #[crate_id="cargo-verify-project"]; +#[allow(deprecated_owned_vector)]; extern crate toml; extern crate getopts; diff --git a/src/cargo/mod.rs b/src/cargo/mod.rs index 837b0ba9b..8ea9d9507 100644 --- a/src/cargo/mod.rs +++ b/src/cargo/mod.rs @@ -1,6 +1,8 @@ #[crate_id="cargo"]; #[crate_type="rlib"]; +#[allow(deprecated_owned_vector)]; + extern crate serialize; use serialize::{Decoder}; use std::fmt; diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 352b45227..27784a1ec 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -1,2 +1,2 @@ pub use self::process_builder::process; -mod process_builder; +pub mod process_builder; diff --git a/src/cargo/util/process_builder.rs b/src/cargo/util/process_builder.rs index e45063f04..821fa4789 100644 --- a/src/cargo/util/process_builder.rs +++ b/src/cargo/util/process_builder.rs @@ -1,26 +1,63 @@ use std; use std::os; -use std::io::process::{Process,ProcessConfig,InheritFd}; +use std::path::Path; +use std::io::IoResult; +use std::io::process::{Process,ProcessConfig,ProcessOutput}; +use ToCargoError; +use CargoResult; pub struct ProcessBuilder { program: ~str, args: ~[~str], - path: ~[~str] + path: ~[~str], + cwd: Path } +// TODO: Upstream a Windows/Posix branch to Rust proper +static PATH_SEP : &'static str = ":"; + impl ProcessBuilder { pub fn args(mut self, arguments: &[~str]) -> ProcessBuilder { self.args = arguments.to_owned(); self } -} -pub fn process(cmd: &str) -> ProcessBuilder { - ProcessBuilder { program: cmd.to_owned(), args: ~[], path: get_curr_path() } + pub fn extra_path(mut self, path: &str) -> ProcessBuilder { + self.path.push(path.to_owned()); + self + } + + pub fn cwd(mut self, path: Path) -> ProcessBuilder { + self.cwd = path; + self + } + + pub fn exec_with_output(self) -> CargoResult { + let mut config = ProcessConfig::new(); + + println!("cwd: {}", self.cwd.display()); + + config.program = self.program.as_slice(); + config.args = self.args.as_slice(); + config.cwd = Some(&self.cwd); + + let os_path = try!(os::getenv("PATH").to_cargo_error(~"Could not find the PATH environment variable", 1)); + let path = os_path + PATH_SEP + self.path.connect(PATH_SEP); + + let path = [(~"PATH", path)]; + config.env = Some(path.as_slice()); + + println!("{:?}", config); + + Process::configure(config).map(|mut ok| ok.wait_with_output()).to_cargo_error(~"Could not spawn process", 1) + } } -pub fn get_curr_path() -> ~[~str] { - os::getenv("PATH").map(|path| { - path.split(std::path::SEP).map(|seg| seg.to_owned()).collect() - }).unwrap_or(~[]) +pub fn process(cmd: &str) -> ProcessBuilder { + ProcessBuilder { + program: cmd.to_owned(), + args: ~[], + path: ~[], + cwd: os::getcwd() + } } diff --git a/tests/support.rs b/tests/support.rs index 293a28d1e..0c5242dff 100644 --- a/tests/support.rs +++ b/tests/support.rs @@ -49,19 +49,24 @@ impl ProjectBuilder { } } + pub fn root(&self) -> Path { + self.root.clone() + } + pub fn file(mut self, path: &str, body: &str) -> ProjectBuilder { self.files.push(FileBuilder::new(self.root.join(path), body)); self } - pub fn build(self) { + // TODO: return something different than a ProjectBuilder + pub fn build(self) -> ProjectBuilder { match self.build_with_result() { Err(e) => fail!(e), - _ => return + _ => return self } } - pub fn build_with_result(self) -> Result<(), ~str> { + pub fn build_with_result(&self) -> Result<(), ~str> { // First, clean the directory if it already exists try!(self.rm_root()); diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 466853675..48690aa3b 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1,3 +1,4 @@ +use std; use support::project; use cargo; @@ -6,7 +7,7 @@ fn setup() { } test!(cargo_compile_with_explicit_manifest_path { - project("foo") + let p = project("foo") .file("Cargo.toml", r#" [project] @@ -24,13 +25,29 @@ test!(cargo_compile_with_explicit_manifest_path { }"#) .build(); - cargo::util::process("cargo-compile") - .args([]); - // //.extra_path("target/") - // //.cwd("/foo/bar") - // //.exec_with_output() + let output = cargo::util::process("cargo-compile") + .args([~"--manifest-path", ~"Cargo.toml"]) + .extra_path(target_path()) + .cwd(p.root()) + .exec_with_output(); + + match output { + Ok(out) => { + println!("out:\n{}\n", std::str::from_utf8(out.output)); + println!("err:\n{}\n", std::str::from_utf8(out.error)); + }, + Err(e) => println!("err: {}", e) + } + + assert!(p.root().join("target/foo").exists(), "the executable exists"); + + let o = cargo::util::process("foo") + .extra_path(format!("{}", p.root().join("target").display())) + .exec_with_output() + .unwrap(); + + assert_eq!(std::str::from_utf8(o.output).unwrap(), "i am foo\n"); - fail!("not implemented"); // 1) Setup project // 2) Run cargo-compile --manifest-path /tmp/bar/zomg // 3) assertThat(target/foo) exists assertThat("target/foo", isCompiledBin()) @@ -38,3 +55,9 @@ test!(cargo_compile_with_explicit_manifest_path { }) // test!(compiling_project_with_invalid_manifest) + +fn target_path() -> ~str { + std::os::getenv("CARGO_BIN_PATH").unwrap_or_else(|| { + fail!("CARGO_BIN_PATH wasn't set. Cannot continue running test") + }) +} diff --git a/tests/tests.rs b/tests/tests.rs index 6eea1cf89..9e51bc559 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,4 +1,5 @@ #[feature(macro_rules)]; +#[allow(deprecated_owned_vector)]; extern crate cargo; -- 2.30.2